
//
//  MineHeadView.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/11.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "MineHeadView.h"
#import "BLHeader.h"
#import "BLHeader.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIButton+WebCache.h>

@interface MineHeadView ()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UIButton *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *signLabel;

@end

@implementation MineHeadView

+ (instancetype) share {
    MineHeadView *head = [[[NSBundle mainBundle] loadNibNamed:@"MineHeadView" owner:nil options:nil] lastObject] ;
    return head;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    self.headImageView.layer.cornerRadius = 40;
    self.headImageView.layer.masksToBounds = YES;
    [self setupUI];
}
- (IBAction)updateUserInfo:(id)sender {
    if ([self.delegate respondsToSelector:@selector(modifyUserInfo)]) {
        [self.delegate modifyUserInfo];
    }
}

- (IBAction)updateUser:(id)sender {
    if ([self.delegate respondsToSelector:@selector(modifyUserInfo)]) {
        [self.delegate modifyUserInfo];
    }
}

- (void) setupUI {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    
    UIVisualEffectView *effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    NSString *headImageURL = [[NSUserDefaults standardUserDefaults] objectForKey:USERIMG];
   // NSLog(@"image = %@",headImageURL);
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:headImageURL] placeholderImage:[UIImage imageNamed:@"icon"]];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:headImageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"icon"]];
    
    effect.frame = CGRectMake(0, 0, KScreenWidth, 200);
    [self.bgImageView addSubview:effect];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    self.nameLabel.text = [userDefault objectForKey:USERNAME];
    NSString *userSign = [userDefault objectForKey:USERSIGN];
    self.signLabel.text = userSign.length > 0  ? userSign : @"我好像忘记签名了..." ;
}

- (void)updateHead:(UIImage *)image {
    if (!image) {
        self.signLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:USERSIGN];
        self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME];
        return;
    }
    
    [self.headImageView setImage:image forState:UIControlStateNormal];
    self.bgImageView.image = image;
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    effect.frame = CGRectMake(0, 0, KScreenWidth, 200);
    [self.bgImageView addSubview:effect];
}


@end
